package com.shunhao.utils

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.util.Base64
import java.io.*

/**
 * 本地化储存 偏好设置工具类
 *
 * @author kang gui yang
 */
object PreUtils {

    /**
     * 是否第一次启动
     */
    const val FIRST_TIME_KEY = "FIRST_TIME"

    const val REGISTER_ID = "j_register_id"

    const val ADVERTISING_DATA = "advertising"
    
    const val IS_SAVE_CONTACT = "is_save_contact"

    const val IS_FIRST_GUIDE_UI = "is_first_guide"

    private fun getSharedPreferences(context: Context?): SharedPreferences {
        return PreferenceManager.getDefaultSharedPreferences(context)
    }

    /**
     * 判断是否是第一次启动APP
     *
     * @param context
     * @param key
     * @return
     */
    fun isFirstTime(context: Context?, key: String?): Boolean {
        return if (getBoolean(context, key, false)) {
            false
        } else {
            putBoolean(context, key, true)
            true
        }
    }

    fun contains(context: Context?, key: String?): Boolean {
        return getSharedPreferences(context).contains(key)
    }

    fun getInt(context: Context?, key: String?, defaultValue: Int): Int {
        return getSharedPreferences(context).getInt(key, defaultValue)
    }

    fun putInt(context: Context?, key: String?, pValue: Int): Boolean {
        val editor = getSharedPreferences(context).edit()
        editor.putInt(key, pValue)
        return editor.commit()
    }

    fun getLong(context: Context?, key: String?, defaultValue: Long): Long {
        return getSharedPreferences(context).getLong(key, defaultValue)
    }

    fun getLong(context: Context?, key: String?, defaultValue: Long?): Long? {
        return if (getSharedPreferences(context).contains(key)) {
            getSharedPreferences(context).getLong(key, 0)
        } else {
            null
        }
    }

    fun putLong(context: Context?, key: String?, pValue: Long): Boolean {
        val editor = getSharedPreferences(context).edit()
        editor.putLong(key, pValue)
        return editor.commit()
    }

    fun getBoolean(context: Context?, key: String?, defaultValue: Boolean): Boolean {
        return getSharedPreferences(context).getBoolean(key, defaultValue)
    }

    fun putBoolean(context: Context?, key: String?, pValue: Boolean): Boolean {
        val editor = getSharedPreferences(context).edit()
        editor.putBoolean(key, pValue)
        return editor.commit()
    }

    fun getString(context: Context?, key: String?, defaultValue: String?): String? {
        return getSharedPreferences(context).getString(key, defaultValue)
    }

    fun putString(context: Context?, key: String?, pValue: String?): Boolean {
        val editor = getSharedPreferences(context).edit()
        editor.putString(key, pValue)
        return editor.commit()
    }

    /**
     * 针对复杂类型存储<对象>
     *
     * @param key
     * @param objValue
    </对象> */
    fun putObj(context: Context?, key: String?, objValue: Any?) {
        val editor = getSharedPreferences(context).edit()
        val baos = ByteArrayOutputStream()
        var out: ObjectOutputStream? = null
        try {
            out = ObjectOutputStream(baos)
            out.writeObject(objValue)
            val objectVal = String(Base64.encode(baos.toByteArray(), Base64.DEFAULT))
            editor.putString(key, objectVal)
            editor.commit()
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                baos?.close()
                out?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }

    fun <T> getObject(context: Context?, key: String?, clazz: Class<T>?): T? {
        val sp = getSharedPreferences(context)
        if (sp.contains(key)) {
            val objectVal = sp.getString(key, null)
            val buffer = Base64.decode(objectVal, Base64.DEFAULT)
            val bais = ByteArrayInputStream(buffer)
            var ois: ObjectInputStream? = null
            try {
                ois = ObjectInputStream(bais)
                return ois.readObject() as T
            } catch (e: StreamCorruptedException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } finally {
                try {
                    bais?.close()
                    ois?.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return null
    }

    fun remove(context: Context?, key: String?): Boolean {
        val editor = getSharedPreferences(context).edit()
        editor.remove(key)
        return editor.commit()
    }


}